package com.ruoyi.college.controller;

import java.util.*;
import java.util.stream.Collectors;

import com.ruoyi.college.domain.Major;
import com.ruoyi.college.service.IMajorService;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.system.service.ISysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.college.domain.Teacher;
import com.ruoyi.college.service.ITeacherService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 教师信息Controller
 * 
 * @author ruoyi
 * @date 2025-08-24
 */
@Controller
@RequestMapping("/college/teacher")
public class TeacherController extends BaseController
{
    private String prefix = "college/teacher";

    @Autowired
    private ITeacherService teacherService;

    @Autowired
    private IMajorService majorService;

    @RequiresPermissions("college:teacher:view")
    @GetMapping()
    public String teacher(ModelMap mmap)
    {
        //专业信息
        mmap.put("majors", majorService.selectMajorList(new Major()));
        return prefix + "/teacher";
    }

    /**
     * 查询教师信息列表
     */
    @RequiresPermissions("college:teacher:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Teacher teacher)
    {
        startPage();
        List<Teacher> list = teacherService.selectTeacherList(teacher);
        return getDataTable(list);
    }

    /**
     * 导出教师信息列表
     */
    @RequiresPermissions("college:teacher:export")
    @Log(title = "教师信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Teacher teacher)
    {
        List<Teacher> list = teacherService.selectTeacherList(teacher);
        ExcelUtil<Teacher> util = new ExcelUtil<Teacher>(Teacher.class);
        return util.exportExcel(list, "教师信息数据");
    }

    /**
     * 新增教师信息
     */
    @RequiresPermissions("college:teacher:add")
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        //专业信息
        Map<Integer, String> map = new HashMap<>();
        map.put(0, "否");
        map.put(1, "是");
        mmap.put("isActives", map);
        mmap.put("majors", majorService.selectMajorList(new Major()));
        return prefix + "/add";
    }

    /**
     * 新增保存教师信息
     */
    @RequiresPermissions("college:teacher:add")
    @Log(title = "教师信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Teacher teacher)
    {
        Integer majorId = teacher.getMajorId();
        Major major = majorService.selectMajorByMajorId(Long.parseLong(String.valueOf(majorId)));
        teacher.setMajorName(major.getMajorName());
        return toAjax(teacherService.insertTeacher(teacher));
    }

    /**
     * 修改教师信息
     */
    @RequiresPermissions("college:teacher:edit")
    @GetMapping("/edit/{teacherId}")
    public String edit(@PathVariable("teacherId") Long teacherId, ModelMap mmap)
    {
        //专业信息
        mmap.put("majors", majorService.selectMajorList(new Major()));
        Teacher teacher = teacherService.selectTeacherByTeacherId(teacherId);
        mmap.put("teacher", teacher);
        return prefix + "/edit";
    }

    /**
     * 修改保存教师信息
     */
    @RequiresPermissions("college:teacher:edit")
    @Log(title = "教师信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Teacher teacher)
    {
        Integer majorId = teacher.getMajorId();
        Major major = majorService.selectMajorByMajorId(Long.parseLong(String.valueOf(majorId)));
        teacher.setMajorName(major.getMajorName());
        return toAjax(teacherService.updateTeacher(teacher));
    }

    /**
     * 删除教师信息
     */
    @RequiresPermissions("college:teacher:remove")
    @Log(title = "教师信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(teacherService.deleteTeacherByTeacherIds(ids));
    }

    @RequiresPermissions("college:teacher:import")
    @Log(title = "教师信息", businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<Teacher> util = new ExcelUtil<Teacher>(Teacher.class);
        List<Teacher> userList = util.importExcel(file.getInputStream());
        String operName = ShiroUtils.getSysUser().getLoginName();
        String message = teacherService.insertBatchTeacher(userList, updateSupport, operName);
        return AjaxResult.success(message);
    }
    /**
     * 导出教师模板
     */
    @RequiresPermissions("college:teacher:exportTemplate")
    @Log(title = "导出教师模板", businessType = BusinessType.EXPORT)
    @GetMapping("/exportTemplate")
    @ResponseBody
    public AjaxResult exportTemplate()
    {
        ExcelUtil<Teacher> util = new ExcelUtil<>(Teacher.class);
        // 创建一个空的教师列表用于生成模板
        List<Teacher> list = Collections.emptyList();
        // 获取所有专业名称用于下拉框选项
        List<Major> majors = majorService.selectMajorList(new Major());
        List<String> majorNames = majors.stream().map(Major::getMajorName).collect(Collectors.toList());

        return util.exportExcelWithDropdowns(list, "教师信息数据", createDropdownMap(majorNames), "教师模板.xlsx");    }

    /**
     * 创建下拉框映射关系
     *
     * @param majorNames 专业名称列表
     * @return 下拉框映射
     */
    private Map<String, List<String>> createDropdownMap(List<String> majorNames) {
        Map<String, List<String>> dropdownMap = new HashMap<>();
        // 假设"专业名称"列需要添加下拉框
        dropdownMap.put("专业名称", majorNames);
        // 可以继续添加其他字段的下拉框选项
        List<String> otherOptions = Arrays.asList("在职", "离职");
         dropdownMap.put("是否在职", otherOptions);
        return dropdownMap;
    }

}
